<template>
  <div class="span" >
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.search' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <input  type="number" class="form-control" v-model="model.f_userinfo_id" placeholder='用户编号'
                    condition="f_userinfo_id = '{}'" v-next-el='yhxm'
                    :size="model.f_userinfo_id ? model.f_userinfo_id.length  : 4">
          </div>
          <div class="form-group">
            <input  type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
                    condition="f_user_name like '%{}%'" v-next-el='resentialarea' v-el:yhxm
                    :size="model.f_user_name ? model.f_user_name.length  : 4">
          </div>
          <div class="form-group">
            <input  type="text" class="form-control" v-model="model.f_address" placeholder='用户地址'
                    condition="f_address like '%{}%'" v-next-el='cx' v-el:resentialarea
                    :size="model.f_address ? model.f_address.length * 2 : 4">
          </div>
          <div class="form-group">
            <input  type="text" class="form-control" v-model="model.f_meterbook_id" placeholder='表册号'
                    condition="f_meterbook_id = '{}'"
                    :size="model.f_meterbook_id ? model.f_meterbook_id.length : 2">
          </div>
          <div class="form-group">
            <button class="btn btn-success" @click="search()" >查询</button>
            <button class="btn btn-primary" @click="$parent.$parent.addToTotal()" >添加至总表</button>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox"  @click="$parent.$parent.$parent.isAll(model.pageIndex)">
            </th>
            <th>序号</th>
            <th>用户姓名</th>
            <th>表编号</th>
            <th>表品牌</th>
            <th>表型号</th>
            <!-- <th>是否总表</th> -->
          </tr>
        </template>
        <template partial='body'>
          <td>
            <input type="checkbox" v-model="$parent.$parent.$parent.selectedDatas"
                   :value="row">
          </td>
          <td>{{$index+1}}</td>
          <td>{{row.f_user_name}}</td>
          <td>{{row.f_user_id}}</td>
          <td>{{row.f_meter_brand}}</td>
          <td>{{row.f_meter_style}}</td>
          <!-- <td style="text-align:center">{{row.f_totalsplit_type}}</td> -->
        </template>
      </data-grid>
    </criteria-paged>
    <modal :show.sync="shareratio" v-ref:modal backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="close"><span>&times;</span></button>
        <h4 class="modal-title">承担系数设置</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="panel panel-info">
          <div class="panel-heading">
        		<h3 class="panel-title">总表信息</h3>
        	</div>
          <div class="panel-body">
            <table class="table">
              <thead>
                <tr>
                   <th>用户姓名</th>
                   <th>表编号</th>
                   <th>表品牌</th>
                   <th>表型号</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                   <td class= "sharetd">{{data.f_user_name}}</td>
                   <td class= "sharetd">{{data.f_user_id}}</td>
                   <td class= "sharetd">{{data.f_meter_brand}}</td>
                   <td class= "sharetd">{{data.f_meter_style}}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <div class="panel panel-info">
          <div class="panel-heading">
        		<h3 class="panel-title">分表信息</h3>
        	</div>
          <div class="panel-body">
            <table class="table pre-scrollable">
              <thead>
                <tr>
                   <th>用户姓名</th>
                   <th>表编号</th>
                   <th>表品牌</th>
                   <th>表型号</th>
                   <th>承担比率</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="single in selectedDatas">
                   <td class= "sharetd">{{single.f_user_name}}</td>
                   <td class= "sharetd">{{single.f_user_id}}</td>
                   <td class= "sharetd">{{single.f_meter_brand}}</td>
                   <td class= "sharetd">{{single.f_meter_style}}</td>
                   <td class= "sharetd">
                     <div class="input-group">
                  		  <input type="number" class="form-control" v-model="single.f_share_ratio" value='0'/>
                  		  <span class="input-group-addon">%</span>
                  	 </div>
                   </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </article>

      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-default" @click='close()'>取消</button>
        <button type="button" class="btn btn-success" @click='save()'>确认</button>
      </footer>
    </modal>
  </div>
</template>

<script>
  /**
   *阶梯水价查询列表
   */
  import { PagedList } from 'vue-client'
  import * as Util from '../../../Util'
  export default {
    data () {
      return {
        model: new PagedList('rs/sql/singleTableOrderBy_WithView', 50, {orderitem: '"' + this.orderitem + '"', items: '"' + this.items + '"',
          tablename: '"' + this.tablename + '"'}),
        shareratio: false,
        selectedDatas: [],
        pages: []
      }
    },
    props: {
      data: ['data'],
      orderitem: {
        type: String,
        default: 'f_userinfo_id'
      },
      tablename: {
        type: String,
        default: 'V_user_table'
      },
      items: {
        type: String,
        default: 'f_user_name,f_user_id,f_meter_brand,f_meter_style'
      }
    },
    ready () {
      this.search({condition: '1=1', model: this.model})
    },
    methods: {
      search (args) {
        args.condition = `${args.condition} and f_totalsplit_type is null`
        if (!Util.f.parent.parent.name.includes('统一资源管理')) {
          args.condition = `${args.condition} and f_filiale = '${Util.f.f_fengongsi}'`
        }
        this.model.search(args.condition, args.model)
      },
      isAll (page) {
        if (this.pages.includes(page)) { // 已添加,取消动作
          // 从ids中删除本页所有数据的ids
          this.addOrRemove(false)
          this.pages = this.pages.filter((item) => {
            return item !== page
          })
        } else { // 未添加
          this.pages.push(page) // 添加id，添加动作
          this.addOrRemove(true)
        }
      },
      addOrRemove (is) {
        let that = this
        console.log('看看有没有总分表', that.data, that.model.rows)
        if (is) { // 添加本页所有数据到ids中
          that.model.rows.forEach((row) => {
            if (!that.selectedDatas.includes(row)) {
              that.selectedDatas.push(row)
            }
          })
        } else { // 从ids中取消所有的数据
          that.model.rows.forEach((row) => {
            that.selectedDatas = that.selectedDatas.filter((item) => {
              return item.f_user_id !== row.f_user_id
            })
          })
        }
        console.log('this.selectedDatas', that.selectedDatas)
      },
      addToTotal () {
        if (!this.data) {
          console.log('总表没有选')
          this.$showAlert('请先选择总表！！！', 'warning', 2000)
        } else {
          this.selectedDatas = this.selectedDatas.filter((item) => {
            return item.f_user_id !== this.data.f_user_id
          })
          this.shareratio = true
        }
      },
      close () {
        this.shareratio = false
      },
      save () {
        console.log('确定了，，，', this.selectedDatas)
        this.$showMessage('确定建立它们之间的总分表关系吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.shareratio = false
            this.$LogicService.addSplit(this.data.f_user_id, this.selectedDatas).then((res) => {
              this.$dispatch('refresh')
            }).catch((error) => {
              this.$dispatch('refresh')
              throw error
            })
            this.selectedDatas = []
          } else if (res === 'cancel') {
            return
          }
        })
      }
    }
  }
</script>
<style media="screen" scoped>
  .sharetd {
    text-align: center;
  }
</style>
